//
//  RefuseViewController.m
//  Contractor
//
//  Created by 金鑫 on 16/6/16.
//  Copyright © 2016年 star j. All rights reserved.
//

#import "RefuseViewController.h"
#import "SheetViewController.h"

@interface RefuseViewController ()<UITextViewDelegate>
{
    UITextView *textview ;
    UILabel *holdLabel ;
    UILabel *numLabel ;
    UIButton *btn ;
}
@end

@implementation RefuseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBarHidden = NO ;
    self.navigationItem.title = @"拒单原因" ;
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.edgesForExtendedLayout = UIRectEdgeBottom | UIRectEdgeLeft | UIRectEdgeRight;
    self.view.backgroundColor = [UIColor lightGrayColor] ;
    [self creatView] ;
}

- (void)creatView
{
    //白色背景图
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, __kScreenWidth, __kScreenWidth*88/300 + 20)] ;
    view.backgroundColor = [UIColor whiteColor] ;
    view.userInteractionEnabled = YES ;
    [self.view addSubview:view] ;
    //灰色背景图
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 10, __kScreenWidth - 40, (__kScreenWidth)*88/300)] ;
    [imageView setImage:[UIImage imageNamed:@"button_refuse_normal_bg"]] ;
    [view addSubview:imageView] ;
    [view sendSubviewToBack:imageView] ;
    
    //初始化并定义大小
    textview = [[UITextView alloc] initWithFrame:CGRectMake(20, 10, __kScreenWidth - 40, (__kScreenWidth)*88/300)];
    textview.backgroundColor=[UIColor clearColor]; //背景色
    textview.layer.cornerRadius = 10.0f ;
    textview.layer.masksToBounds = YES ;
    textview.scrollEnabled = NO;    //当文字超过视图的边框时是否允许滑动，默认为“YES”
    textview.editable = YES;        //是否允许编辑内容，默认为“YES”
    textview.delegate = self;       //设置代理方法的实现类
    textview.font=[UIFont fontWithName:@"Arial" size:16.0]; //设置字体名字和字体大小;
    textview.returnKeyType = UIReturnKeyDefault;//return键的类型
    textview.keyboardType = UIKeyboardTypeDefault;//键盘类型
    textview.textAlignment = NSTextAlignmentLeft; //文本显示的位置默认为居左
    textview.dataDetectorTypes = UIDataDetectorTypeAll; //显示数据类型的连接模式（如电话号码、网址、地址等）
    textview.textColor = [UIColor blackColor];
    [view addSubview:textview];
    
    holdLabel = [[UILabel alloc] init] ;
    holdLabel.frame = CGRectMake(27, 18, 200, 18.0f) ;
    holdLabel.text = @"输入拒单原因" ;
    holdLabel.enabled = NO ;
    holdLabel.backgroundColor = [UIColor clearColor] ;
    holdLabel.textColor = [UIColor blackColor] ;
    [view addSubview:holdLabel] ;
    
    numLabel = [[UILabel alloc] initWithFrame:CGRectMake(__kScreenWidth - 177, view.frame.size.height - 10 - 8 - 18.0f, 150, 18.0f)] ;
    numLabel.textColor = [UIColor blackColor] ;
    numLabel.text = @"0/50" ;
    numLabel.enabled = NO ;
    numLabel.textAlignment = NSTextAlignmentRight ;
    [view addSubview:numLabel] ;
    
    btn = [UIButton buttonWithType:UIButtonTypeCustom] ;
    btn.frame = CGRectMake(20, view.frame.size.height + 20, __kScreenWidth - 40, 40) ;
    btn.layer.cornerRadius = 6.0f ;
    [btn setBackgroundColor:[UIColor blueColor]] ;
    [btn setTitle:@"确认" forState:UIControlStateNormal] ;
    [btn addTarget:self action:@selector(downloadOfRufush) forControlEvents:UIControlEventTouchUpInside] ;
    [self.view addSubview:btn] ;

}

#pragma mark - textviewDelegate
- (void)textViewDidChange:(UITextView *)textView
{
    if (textview.text.length == 0) {
        numLabel.text = @"0/50" ;
        holdLabel.text = @"输入拒单原因" ;
    }
    else{
        numLabel.text = [NSString stringWithFormat:@"%lu/50",(unsigned long)textView.text.length] ;
        holdLabel.text = @"" ;
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if (range.location>=50)
    {
        //控制输入文本的长度
        return  NO;
    }
    if ([text isEqualToString:@"\n"]) {
        //禁止输入换行
        [textView resignFirstResponder] ;
        return NO;
    }
    else
    {
        return YES;
    }
}

#pragma mark - 拒单接口
- (void)downloadOfRufush
{
    if (textview.text.length == 0) {
        btn.enabled = NO ;
        UILabel *label = [[UILabel alloc] init] ;
        label.font = [UIFont systemFontOfSize:14.0f] ;
        label.text = @"输入不能为空！" ;
        label.textColor = [UIColor whiteColor] ;
        label.textAlignment = NSTextAlignmentCenter ;
        label.backgroundColor = [UIColor blackColor] ;
        label.alpha = 0.8 ;
        label.layer.cornerRadius = 10 ;
        label.layer.masksToBounds = YES ;
        label.frame = CGRectMake((__kScreenWidth - 150) / 2, self.view.frame.size.height - 100, 150, 40) ;
        label.numberOfLines = 0;
        [self.view addSubview:label] ;
        [self performSelector:@selector(clearbtn:) withObject:label afterDelay:2.0] ;
    }
    else{
        NSUserDefaults *uidDefaults = [NSUserDefaults standardUserDefaults] ;
        NSInteger uidInt = [[uidDefaults valueForKey:@"uid"] integerValue] ;
        
        NSNumber *cmd = [[NSNumber alloc] initWithInt:206] ;
        NSNumber *uid = [[NSNumber alloc] initWithInteger:uidInt] ;
        
        
        NSNumber *accepttype = [[NSNumber alloc] initWithInt:0] ;
        
        NSDictionary *jsondic = @{@"gdorderid":_ordid,
                                  @"accepttype":accepttype,
                                  @"memo":textview.text
                                  } ;
        NSLog(@"%@",jsondic) ;
        
        NSString *jsonstr =  [CodeWithDES encodeWithDic:jsondic] ;
        NSLog(@"%@",jsondic) ;
        NSLog(@"%@",jsonstr) ;
        NSDictionary *parameters = @{@"cmd":cmd, @"uid":uid, @"jsonstr":jsonstr};
        
        NSLog(@"parameters:%@",parameters) ;
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        [manager POST:getAppJson parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
         {
             //解密并解析json数据，将json转成字典
             NSString *decodeStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding] ;
             NSLog(@"decodeStr:%@",decodeStr) ;
             
             decodeStr = [CodeWithDES decodeWithStr:decodeStr] ;
             NSLog(@"decodeStr:%@",decodeStr) ;
             NSData *decodeData = [decodeStr dataUsingEncoding:NSUTF8StringEncoding] ;
             id result = [NSJSONSerialization JSONObjectWithData:decodeData options:NSJSONReadingMutableContainers error:nil] ;
             //获取字典中的值
             if ([result isKindOfClass:[NSDictionary class]])
             {
                 NSDictionary *dic = result ;
                 NSLog(@"通知:%@",dic) ;
                 if ([dic[@"status"]integerValue] == 1) {
                     SheetViewController *vc = [[SheetViewController alloc] init] ;
                     self.hidesBottomBarWhenPushed = NO ;
                     [self.navigationController pushViewController:vc animated:NO] ;
                 }
             }
         }
              failure:^(AFHTTPRequestOperation *operation, NSError *error)
         {
             NSLog(@"Error: %@", error);
         }];
    }
    
}


- (void)clearbtn:(id)sender
{
    btn.enabled = YES ;
    
    [sender removeFromSuperview] ;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
